package com.adopapa.nezhacloud.authc.controller;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.Map;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.adopapa.nezhacloud.authc.domain.AddressInfo;
import com.adopapa.nezhacloud.authc.service.AddressInfoService;
import com.adopapa.nezhacloud.common.Tools;
import com.adopapa.nezhacloud.common.RestResponse;
import com.adopapa.nezhacloud.ddd.codegen.SqlGen;
import com.adopapa.nezhacloud.ddd.controller.BaseController;
import com.adopapa.nezhacloud.ddd.conf.AppConstants;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "")
@RestController
@RequestMapping("/authc/addressinfo")
public class AddressInfoController extends BaseController {

	@Autowired
	private PasswordEncoder passwordEncoder;
	@Autowired
	private AddressInfoService addressInfoService;

	@Operation(summary = "分页查询")
    @SqlGen(name = { "", "", "分页查询" }, method = SqlGen.METHOD_GET, path = "/addressinfos/{pageNo}")
	@GetMapping(path = "/addressinfos/{pageNo}", produces = MediaType.APPLICATION_JSON_VALUE)
	public RestResponse getAddressInfoPage(@RequestHeader String authorization, //
			@PathVariable Integer pageNo, //
			@RequestParam(required = false) String loginName, //
			@RequestParam(required = false) String loginPwd, //
			@RequestParam(required = false) String userName, //
			@RequestParam(required = false) String remark, //
			@RequestParam(required = false) Integer pageSize) {

		AddressInfo addressInfo = new AddressInfo();
		
		addressInfo.setLoginName(loginName);	
		addressInfo.setLoginPwd(loginPwd);	
		addressInfo.setUserName(userName);	
		addressInfo.setRemark(remark);	

		pageSize = Tools.empty(pageSize) ? AppConstants.DEFAULT_PAGE_SIZE : pageSize;

		Page<AddressInfo> page = addressInfoService.getAddressInfoPage(pageNo, pageSize, addressInfo);

		return RestResponse.ok(page);
	}

	@Operation(summary = "单个查询")
    @SqlGen(name = { "", "", "" }, method = SqlGen.METHOD_GET, path = "/addressinfo/{id}")
	@GetMapping(path = "/addressinfo/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
	public RestResponse getAddressInfo(@RequestHeader String authorization, //
			@PathVariable String id) {
		AddressInfo addressInfo = addressInfoService.getAddressInfoById(id);
		return RestResponse.ok(addressInfo);
	}

	@Operation(summary = "编辑")
	@io.swagger.v3.oas.annotations.parameters.RequestBody(content = {
			@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
					@ExampleObject("{}")
			} )
	})
    @SqlGen(name = { "", "", "编辑" }, method = SqlGen.METHOD_POST, path = "/addressinfos")
	@PostMapping(path = "/addressinfos", produces = MediaType.APPLICATION_JSON_VALUE, //
			consumes = MediaType.APPLICATION_JSON_VALUE)
	public RestResponse saveAddressInfo(@RequestHeader String authorization, //
			@RequestBody Map<String, Object> params) {

		String id = getMapValue(params, "id", String.class);
		
		String loginName = getMapValue(params, "loginName", String.class);
		String loginPwd = getMapValue(params, "loginPwd", String.class);
		String userName = getMapValue(params, "userName", String.class);
		String remark = getMapValue(params, "remark", String.class);
	
		if(Tools.empty(loginName)) {
			throw new RuntimeException("用户名称不能为空");
	    }
		if(Tools.empty(loginPwd)) {
			throw new RuntimeException("用户密码不能为空");
	    }
		if(Tools.empty(userName)) {
			throw new RuntimeException("姓名不能为空");
	    }
	
		AddressInfo addressInfo = null;
		if (Tools.empty(id)) {
			addressInfo = new AddressInfo();
		} else {
			addressInfo = addressInfoService.getAddressInfoById(id);
			if (Tools.empty(addressInfo)) {
				throw new RuntimeException("指定id对应信息不存在");
			}
		}
		AddressInfo existed = addressInfoService.getAddressInfoByLoginName(loginName);
		if (!Tools.empty(existed) && !existed.getId().equals(id)) {
			throw new RuntimeException("loginName已经存在");
		}
		
		addressInfo.setLoginName(loginName);
		addressInfo.setLoginPwd(loginPwd);
		addressInfo.setUserName(userName);
		addressInfo.setRemark(remark);

		addressInfoService.saveAddressInfo(addressInfo);

		return RestResponse.ok();

	}

	@Operation(summary = "删除")
    @SqlGen(name = { "", "", "删除" }, method = SqlGen.METHOD_DELETE, path = "/addressinfo/{id}")
	@DeleteMapping(path = "/addressinfo/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
	public RestResponse deleteAddressInfo(@RequestHeader String authorization, //
			@PathVariable String id) {
		try {
			addressInfoService.deleteAddressInfo(id);
		} catch (Exception e) {
			logger.error(ExceptionUtils.getStackTrace(e));
		}
		
		return RestResponse.ok();
	}

}
